package keys

import "fmt"
import "x-go-binding.googlecode.com/hg/xgb"


const XK_space = 0x0020  
const XK_exclam = 0x0021  
const XK_quotedbl = 0x0022  
const XK_numbersign = 0x0023  
const XK_dollar = 0x0024  
const XK_percent = 0x0025  
const XK_ampersand = 0x0026  
const XK_apostrophe = 0x0027  
const XK_quoteright = 0x0027  
const XK_parenleft = 0x0028  
const XK_parenright = 0x0029  
const XK_asterisk = 0x002a  
const XK_plus = 0x002b  
const XK_comma = 0x002c  
const XK_minus = 0x002d  
const XK_period = 0x002e  
const XK_slash = 0x002f  
const XK_0 = 0x0030  
const XK_1 = 0x0031  
const XK_2 = 0x0032  
const XK_3 = 0x0033  
const XK_4 = 0x0034  
const XK_5 = 0x0035  
const XK_6 = 0x0036  
const XK_7 = 0x0037  
const XK_8 = 0x0038  
const XK_9 = 0x0039  
const XK_colon = 0x003a  
const XK_semicolon = 0x003b  
const XK_less = 0x003c  
const XK_equal = 0x003d  
const XK_greater = 0x003e  
const XK_question = 0x003f  
const XK_at = 0x0040  
const XK_A = 0x0041  
const XK_B = 0x0042  
const XK_C = 0x0043  
const XK_D = 0x0044  
const XK_E = 0x0045  
const XK_F = 0x0046  
const XK_G = 0x0047  
const XK_H = 0x0048  
const XK_I = 0x0049  
const XK_J = 0x004a  
const XK_K = 0x004b  
const XK_L = 0x004c  
const XK_M = 0x004d  
const XK_N = 0x004e  
const XK_O = 0x004f  
const XK_P = 0x0050  
const XK_Q = 0x0051  
const XK_R = 0x0052  
const XK_S = 0x0053  
const XK_T = 0x0054  
const XK_U = 0x0055  
const XK_V = 0x0056  
const XK_W = 0x0057  
const XK_X = 0x0058  
const XK_Y = 0x0059  
const XK_Z = 0x005a  
const XK_bracketleft = 0x005b  
const XK_backslash = 0x005c  
const XK_bracketright = 0x005d  
const XK_asciicircum = 0x005e  
const XK_underscore = 0x005f  
const XK_grave = 0x0060  
const XK_quoteleft = 0x0060  
const XK_a = 0x0061  
const XK_b = 0x0062  
const XK_c = 0x0063  
const XK_d = 0x0064  
const XK_e = 0x0065  
const XK_f = 0x0066  
const XK_g = 0x0067  
const XK_h = 0x0068  
const XK_i = 0x0069  
const XK_j = 0x006a  
const XK_k = 0x006b  
const XK_l = 0x006c  
const XK_m = 0x006d  
const XK_n = 0x006e  
const XK_o = 0x006f  
const XK_p = 0x0070  
const XK_q = 0x0071  
const XK_r = 0x0072  
const XK_s = 0x0073  
const XK_t = 0x0074  
const XK_u = 0x0075  
const XK_v = 0x0076  
const XK_w = 0x0077  
const XK_x = 0x0078  
const XK_y = 0x0079  
const XK_z = 0x007a  
const XK_braceleft = 0x007b  
const XK_bar = 0x007c  
const XK_braceright = 0x007d  
const XK_asciitilde = 0x007e  

const XK_Home     =                     0xff50
const XK_Left      =                    0xff51  /* Move left, left arrow */
const XK_Up         =                   0xff52  /* Move up, up arrow */
const XK_Right       =                  0xff53  /* Move right, right arrow */
const XK_Down         =                 0xff54  /* Move down, down arrow */
const XK_Prior         =                0xff55  /* Prior, previous */
const XK_Page_Up        =               0xff55
const XK_Next            =              0xff56  /* Next */
const XK_Page_Down        =             0xff56
const XK_End               =            0xff57  /* EOL */
const XK_Begin              =           0xff58  /* BOL */


const XK_BackSpace = 0xff08  /* Back space, back char */
const XK_Tab        =                   0xff09
const XK_Linefeed    =                  0xff0a  /* Linefeed, LF */
const XK_Clear        =                 0xff0b
const XK_Return        =                0xff0d  /* Return, enter */
const XK_Pause          =               0xff13  /* Pause, hold */
const XK_Scroll_Lock     =              0xff14
const XK_Sys_Req          =             0xff15
const XK_Escape            =            0xff1b
const XK_Delete             =           0xffff  /* Delete, rubout */

const XK_Shift_L      =                 0xffe1  /* Left shift */
const XK_Shift_R       =                0xffe2  /* Right shift */
const XK_Control_L      =               0xffe3  /* Left control */
const XK_Control_R       =              0xffe4  /* Right control */
const XK_Caps_Lock        =             0xffe5  /* Caps lock */
const XK_Shift_Lock        =            0xffe6  /* Shift lock */

const XK_F1         =                   0xffbe
const XK_F2          =                  0xffbf
const XK_F3           =                 0xffc0
const XK_F4            =                0xffc1
const XK_F5             =               0xffc2
const XK_F6              =              0xffc3
const XK_F7               =             0xffc4
const XK_F8                =            0xffc5
const XK_F9                 =           0xffc6
const XK_F10 = 0xffc7

var keymap = [...]int{
    0,                // 0x00:
    0,                // 0x01:
    0,                // 0x02:
    0,                // 0x03:
    0,                // 0x04:
    0,                // 0x05:
    0,                // 0x06:
    0,                // 0x07:
    0,                // 0x08:
    XK_Escape,        // 0x09: XK_Escape
    XK_1,             // 0x0A: XK_1
    XK_2,             // 0x0B: XK_2
    XK_3,             // 0x0C: XK_3
    XK_4,             // 0x0D: XK_4
    XK_5,             // 0x0E: XK_5
    XK_6,             // 0x0F: XK_6
    XK_7,             // 0x10: XK_7
    XK_8,             // 0x11: XK_8
    XK_9,             // 0x12: XK_9
    XK_0,             // 0x13: XK_0
    XK_minus,         // 0x14: XK_minus
    XK_equal,         // 0x15: XK_equal
    XK_BackSpace,     // 0x16: XK_BackSpace
    XK_Tab,           // 0x17: XK_Tab
    XK_q,             // 0x18: XK_q
    XK_w,             // 0x19: XK_w
    XK_e,             // 0x1A: XK_e
    XK_r,             // 0x1B: XK_r
    XK_t,             // 0x1C: XK_t
    XK_y,             // 0x1D: XK_y
    XK_u,             // 0x1E: XK_u
    XK_i,             // 0x1F: XK_i
    XK_o,             // 0x20: XK_o
    XK_p,             // 0x21: XK_p
    XK_bracketleft,   // 0x22: XK_bracketleft
    XK_bracketright,  // 0x23: XK_bracketright
    XK_Return,        // 0x24: XK_Return
    XK_Control_L,     // 0x25: XK_Control_L
    XK_a,             // 0x26: XK_a
    XK_s,             // 0x27: XK_s
    XK_d,             // 0x28: XK_d
    XK_f,             // 0x29: XK_f
    XK_g,             // 0x2A: XK_g
    XK_h,             // 0x2B: XK_h
    XK_j,             // 0x2C: XK_j
    XK_k,             // 0x2D: XK_k
    XK_l,             // 0x2E: XK_l
    XK_semicolon,     // 0x2F: XK_semicolon
    XK_apostrophe,    // 0x30: XK_apostrophe
    XK_grave,         // 0x31: XK_grave
    XK_Shift_L,       // 0x32: XK_Shift_L
    XK_backslash,     // 0x33: XK_backslash
    XK_z,             // 0x34: XK_z
    XK_x,             // 0x35: XK_x
    XK_c,             // 0x36: XK_c
    XK_v,             // 0x37: XK_v
    XK_b,             // 0x38: XK_b
    XK_n,             // 0x39: XK_n
    XK_m,             // 0x3A: XK_m
    XK_comma,         // 0x3B: XK_comma
    XK_period,        // 0x3C: XK_period
    XK_slash,         // 0x3D: XK_slash
    XK_Shift_R,       // 0x3E: XK_Shift_R
    0,                // 0x3F: XK_KP_Multiply
    0,         // 0x40: XK_Alt_L
    XK_space,         // 0x41: XK_space
    XK_Caps_Lock,     // 0x42: XK_Caps_Lock
    XK_F1,            // 0x43: XK_F1
    XK_F2,            // 0x44: XK_F2
    XK_F3,            // 0x45: XK_F3
    XK_F4,            // 0x46: XK_F4
    XK_F5,            // 0x47: XK_F5
    XK_F6,            // 0x48: XK_F6
    XK_F7,            // 0x49: XK_F7
    XK_F8,            // 0x4A: XK_F8
    XK_F9,            // 0x4B: XK_F9
    XK_F10,           // 0x4C: XK_F10
    // XK_Num_Lock,      // 0x4D: XK_Num_Lock
    // XK_Scroll_Lock,   // 0x4E: XK_Scroll_Lock
  }

func KeyCodeToChar(event *xgb.KeyReleaseEvent) int {
	simple := keycodeFor(event.Detail)
	// fmt.Println("State: ", event.State)
	if (event.State & xgb.ModMaskShift) != 0 { simple = simple - 'a' + 'A' }
	if (event.State & xgb.ModMaskControl) != 0 { simple = simple - 'a' + 1 }
	return simple;
}

func keycodeFor(detail byte) int {
	// mt.Println("kcctc: detail = ", detail)
	if int(detail) < len(keymap) { return keymap[detail] }
	if detail == 111 { return XK_Up }
	if detail == 116 { return XK_Down }
	if detail == 114 { return XK_Right }
	if detail == 113 { return XK_Left }
	if detail == 119 { return XK_Delete }
	fmt.Println("detail ", int64(detail), " unrecognised")
	return 0
}